<template>
  <div class="app-container">
    <div class="searchBox">
      <el-form :inline="true">
        <el-form-item label="标题：">
          <el-input v-model="search.title" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="消息类型：">
          <el-select v-model="search.messageType" placeholder="请选择">
            <el-option
              v-for="item in controlData.messageTypes"
              :key="item.typeId"
              :label="item.name"
              :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="消息状态：">
          <el-select v-model="search.status" placeholder="请选择">
            <el-option
              v-for="item in controlData.messageStatus"
              :key="item.value"
              :label="item.key"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="searchQuery">查询</el-button>
          <el-button
            type="info"
            @click="
              search = {};
              searchQuery();
            "
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <qm-table
      ref="qmTable"
      :data="query"
      key-prop="messageId"
      :text-map="{ add: '发送消息' }"
      :menuOptions="{ width: 100 }"
      menuType="text"
      :operMenuRenderFuncMap="{
          remove: (row) => true,
          edit: (row) => {return row.status==='waiting'},
          view: (row) => {return row.status==='processed'||row.status==='exception'}
      }"
      useFormPage
       :formPage="{
        create: '/message/sysMessage/create',
        view: '/message/sysMessage/view',
        edit: '/message/sysMessage/create'
            }"
    >
      <qm-table-column
        field="messageTypeText"
        sortable="true"
      >消息类型</qm-table-column>
      <qm-table-column field="title" sortable="true">标题</qm-table-column>
      <qm-table-column
        field="systemName"
        sortable="true"
        style="width: 185px"
      >应用系统</qm-table-column>
      <qm-table-column
        field="status"
        sortable="true"
        :render-func="renderMessageStatus"
      >状态</qm-table-column>
      <qm-table-column
        field="createdDate"
        field-type="datetime"
        style="width: 140px;"
      >创建时间</qm-table-column>
      <qm-table-column
        field="createdUserName"
        style="width: 88px;"
      >发送人</qm-table-column>

      <template #form="{ models, controlData, disabled,pageAction }">
        <create
          ref="create"
          :models="models"
          :control-data="controlData"
          :disabled="disabled"
          :pageAction="pageAction"
        />
      </template>
    </qm-table>
  </div>
</template>

<script>
import Controller from '@/libs/framework/controller'
import Create from './create'
export default class Instance extends Controller {
  onBeforeInit() {
    this.urls = {
      model: '/message/sysMessage/getListModel',
      query: '/message/sysMessage/query',
      save: '/message/sysMessage/save',
      delete: '/message/sysMessage/delete',
      entity: '/message/sysMessage/getEntity'
    }
  }

  onBeforeSave(postModel) {
   try {
      if (!postModel.model.messageType) {
      this.vm.notifyError('请选择消息类型')
      return false
    }
    if (!postModel.model.systemId) {
      this.vm.notifyError('请选择应用系统')
      return false
    }
    if (postModel.model.priority<0) {
      this.vm.notifyError('请选择优先级')
      return false
    }
    if (this.$util.isArray(postModel.model.users)) {
      const vm = this.vm.$refs['qmTable'].viewModel
      const userNames = []
      const that = this;
      this.$util.each(postModel.model.users, function(item1, ii) {
        const find = that.$util.find(vm.controlData.userList, item => {
          return item.loginId === item1
        })
        if (find && find.userName) {
          userNames.push(find.userName)
        }
      })
      postModel.model.usersName = userNames.join(',')
      postModel.model.users = postModel.model.users.join(',')
    }
     return true
   } catch (error) {
     return false;
   }
  }

   onAfterGetEntity(r){
     if(r.models.model.users!==null){
     r.models.model.users=r.models.model.users.split(",")
     }
  }

  mixin() {
    return {
      components: {
        Create
      },
      data() {
        return {}
      },
      methods: {
        renderMessageStatus(data, row, column) {
          const find = this.super.$util.find(
            this.controlData.messageStatus,
            item => {
              return item.value === data
            }
          )
          return (find || {}).key
        }
      }
    }
  }
}
</script>

<style scoped lang="scss"></style>
